﻿using NHibernate;
using NHibernate.Criterion;
using System.Collections.Generic;
using MSP.Data.Domain;
using MSP.Data.NHibernate;

namespace MSP.Data.Repositories
{
    public class UserRepository : Repository<User>
    {
        public UserRepository(ISession session)
            : base(session)
        {
        }

        public void SaveUser(User user)
        {
            Add(user);
        }

        public void UpdateUser(User user)
        {
            Update(user);
        }

        public void DeleteUser(int Id)
        {
            User user = GetUserById(Id);
            if (user != null)
                Delete(user);
        }

        public string GetUserPassword(long Id)
        {
            return Session.CreateCriteria<User>()
                          .Add(Restrictions.Eq("Id", Id))
                          .UniqueResult<User>().Password;
        }

        public User GetUserById(int Id)
        {
            return Session.CreateCriteria<User>()
                          .Add(Restrictions.Eq("Id", Id))
                          .UniqueResult<User>();
        }

        public User GetUserByUserName(string userName)
        {
            return Session.CreateCriteria<User>()
                          .Add(Restrictions.Eq("Login", userName))
                          .UniqueResult<User>();
        }

        public IList<User> GetUserByEmail(string email)
        {
            return (Session.CreateCriteria<User>()
                          .Add(Restrictions.Eq("Email", email))
                          .List<User>() as List<User>);
        }

        public User ValidateUser(string userName, string password)
        {
            return Session.CreateCriteria<User>()
                          .Add(Restrictions.Eq("Login", userName))
                          .Add(Restrictions.Eq("Password", password))
                          .UniqueResult<User>();
        }

        public IList<User> GetAllUser()
        {
            return (Session.CreateCriteria<User>()
               .List<User>() as List<User>);
        }

    }
}
